﻿package com.designoutput.ui
{
	import com.designoutput.enum.ToolType;
	import com.designoutput.event.CardEditorEvent;
	import com.designoutput.event.CardEvent;
	import com.designoutput.ui.newControls.Window;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	/**
	 * The Card edit area
	 */
	public class CardWindow extends Window
	{
		public var card:Card;
		
		public var editors:Vector.<CardFaceEditor>
		public var activeEditor:CardFaceEditor;
		public var editorsByLayer:Dictionary;
		
		private var _toolType:String;
		
		private var _splitView:Boolean = false;
		
		override protected function initialize():void {
			super.initialize();
			
			x = 160;
			y = 65;
			
			width = 800;
			height = 500;
											
			card = new Card();
			card.addEventListener(CardEvent.CARD_DATA_CHANGED, card_cardDataChangedHandler, false, 0, true);
			card.addEventListener(CardEvent.CARD_TYPE_CHANGED, card_typeChangedHandler, false, 0, true);
			
			editors = new Vector.<CardFaceEditor>();
			editorsByLayer = new Dictionary(true);
			
			addEditor(card.getEditorForLayer(card.layers[0]));
			
			updateTitle();
			
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, true, 0, true);
		}	
			
		protected function mouseDownHandler(e:MouseEvent):void {
			for each(var editor:CardFaceEditor in editors){
				if(editor != activeEditor && editor.contains(e.target as DisplayObject)){
					setActiveEditor(editor);
					editor.layerMouseDown(e);
					break;
				}
			}
		}
		
		override protected function get minWidth():Number {
			return 550;
		}
		
		override protected function get minHeight():Number {
			return 300;
		}
		
		override protected function updateDisplay():void {
			super.updateDisplay();
			
			windowContent.graphics.clear();
			
			var gap:Number = 4;
			
			var editorWidth:Number = int((contentAvailableWidth - ((editors.length - 1) * gap)) / editors.length);
			
			var curX:Number = 0;
			for(var i:int = 0; i < editors.length; i++){
				if(i > 0){
					windowContent.graphics.beginFill(0x222222, 1);
					windowContent.graphics.drawRect(curX, 0, gap, contentAvailableHeight);
					curX += gap;
				}
				
				var editor:CardFaceEditor = editors[i];
				editor.x = curX;
				editor.width = editorWidth;
				editor.height = contentAvailableHeight;
				curX += editorWidth;
			}
		}
		
		public function showRuler(flag:Boolean):void
		{
			for each(var editor:CardFaceEditor in editors){
				editor.showRuler(flag);
			}
		}
		
		public function showCardMargins(flag:Boolean):void
		{
			for each(var editor:CardFaceEditor in editors){
				editor.showMargins = flag;
			}
		}
	
		protected function card_cardDataChangedHandler(e:CardEvent):void {
			updateTitle();
		}
		
		protected function card_typeChangedHandler(e:Event):void {
			updateTitle();
			
			for each(var editor:CardFaceEditor in editors){
				if(card.layers.indexOf(editor.layer) == -1)
					editor.setLayer(null);
			}
		}
		
		protected function updateTitle():void {
			var title:String;
			
			if(card.cardId == 0)
				title = "New ";
			else
				title = "[" + card.cardId + "] " + card.cardName;
			
			if(card.cardType != null)
				title += " - " + card.cardType.name;
			
			setTitle(title);
		}
		
		public function setToolType(type:String=ToolType.None):void
		{
			_toolType = type;
			activeEditor.setTool(type);
		}
		
		

		////////////////////////////////////////////////////
		// Editors manipulation
		////////////////////////////////////////////////////		
		public function get numEditors():int {
			return windowContent.numChildren;
		}
		
		public function addEditor(editor:CardFaceEditor):void {
			windowContent.addChild(editor);
			editors.push(editor);
			
			if(editors.length == 1) {
				setActiveEditor(editor);
			}
			
			editor.viewing = true;
			
			invalidateDisplay();
		}
		
		public function getEditorAt(index:int):CardFaceEditor {
			return windowContent.getChildAt(index) as CardFaceEditor;
		}
		
		public function removeEditorAt(index:int):void {
			removeEditor(getEditorAt(index));
		}
		
		public function removeEditor(editor:CardFaceEditor):void {
			var editorIndex:int = editors.indexOf(editor);
			if(editorIndex != -1){
				editors.splice(editorIndex, 1);
				windowContent.removeChild(editor);
				
				if(activeEditor == editor){
					if(editors.length > 0)
						setActiveEditor(editors[editors.length-1]);
				}
				
				editor.viewing = false;
				
				invalidateDisplay();
			}
		}
		
		
		public function setActiveEditor(editor:CardFaceEditor):void {
			if(activeEditor){
				activeEditor.deactivate();
			}
			
			activeEditor = editor;
			
			if(activeEditor){
				activeEditor.activate();
				activeEditor.setTool(_toolType);
			}
			
			card.changeCurrentLayer(activeEditor.layer);
			dispatchEvent( new CardEditorEvent(CardEditorEvent.CURRENT_EDITOR_CHANGED) );
		}
	}
}